<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row layui-form" id="form">

            <div class="layui-col-lg2">
                <input type="text" name="keyword" placeholder="请输入关键词" autocomplete="off" class="layui-input">
            </div>

            <div class="layui-col-lg2">
                <select name="cat_id">
                    <option value="">分类</option>
                    <?php foreach($cat as $k => $v):?>
                    <option value="<?=$v['id']?>"><?php for($i=2;$i<=$v['depath'];$i++):?>&nbsp;&nbsp;&nbsp;&nbsp;<?php endfor;?>-|<?=$v['cat_title']?></option>
                    <?php endforeach;?>
                </select>
            </div>

            <div class="layui-col-lg2">
                <select name="rec_id">
                    <option value="">推荐位</option>
                    <?php foreach($rec as $k => $v):?>
                    <option value="<?=$v['id']?>"><?=$v['title']?></option>
                    <?php endforeach;?>
                </select>
            </div>

            <div class="layui-col-lg1">
                <select name="status">
                    <option value="">状态</option>
                    <?php foreach(c('type.goods_status') as $k => $v):?>
                    <option value="<?=$k?>"><?=$v?></option>
                    <?php endforeach;?>
                </select>
            </div>

            <div class="layui-col-lg1">
                <button class="layui-btn layui-btn-normal" onclick="search()">搜索</button>
            </div>
        </div>
        <div class="layui-row" style="width: 100%">
            <table class="layui-hide" lay-filter="table" id="table"></table>
        </div>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table', 'form'], function() {
        var form = layui.form;
        var table = layui.table;
        var $ = layui.$;
        form.render('select');
        search = function() {
            var url = "/goods/lists";
            var data = $("#form").find("[name]");
            var where = {};
            data.each(function(i, e) {
                where[$(e).attr('name')] = $(e).val();
            });
            table.render({
                elem: '#table',
                url: url,
                where: where,
                method: 'post',
                cellMinWidth: 180,
                page: true,
                cols: [
                    [
                        { type: 'checkbox' },
                        { field: 'id', title: 'ID', width: 80, sort: true, title: 'ID' },
                        {field:'pic', title: '图片', width: 60,templet: function(d){
                                return "<img height=30 src='"+d.pic+"' />";
                            }},
                        { field: 'title', title: '标题' },
                        { field: 'ptitle', title: '分类' },
                        { field: 'sort', title: '排序', edit: 'text' },
                        { field: 'ctitle', title: '推荐位' },
                        { field: 'status', width: 100, title: '状态' },
                        { field: 'create_time', title: '时间' },
                        { field: 'action', title: '操作', width: 150, align: 'center', toolbar: '#barDemo', fixed: "right", }
                    ]
                ]
            });
        };
        search();
        //监听工具条
        table.on('tool(table)', function(obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.open({
                    title: '查看',
                    type: 2,
                    area: ['100%', '100%'],
                    content: '/goods/detail/id/' + data.id
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    title: '编辑',
                    type: 2,
                    area: ['100%', '100%'],
                    content: '/goods/edit?id='+data.id
                });
            } else if (obj.event === 'del') {
                layer.confirm('确认要删除吗?', function(index) {
                    $.ajax({
                        url: "/goods/del",
                        method: "post",
                        dataType: "json",
                        data: { id: data.id },
                        success: function(res) {
                            my.action(res);
                            layer.close(index);
                        }
                    });
                });
            }
        });
        //监听编辑框
        table.on('edit(table)', function(obj) {
            var data = {};
            data['table'] = 'goods';
            data['id'] = obj.data.id;
            data['field'] = obj.field;
            data['value'] = obj.value;
            $.ajax({
                url: '/com/edit',
                method: 'post',
                dataType: 'json',
                data: data,
                success: function(res) {
                    my.action(res);
                }
            });
        });
    });
</script>
